import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_cookbook/ui/pages/CookbookListPage.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'package:fluttertoast/fluttertoast.dart';

var _context;
List<Widget> _list = [];

class HomeSearchWidget extends StatelessWidget {

  final TextEditingController _searchController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _context = context;

    return Center(
      child: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          padding: EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                onSubmitted: (value) {
                  // print(value);
                  if (value.trim().isEmpty) {
                    //print("没有内容");

                    Fluttertoast.showToast(
                      msg: "请输入菜谱！",
                      toastLength: Toast.LENGTH_SHORT,
                      gravity: ToastGravity.BOTTOM,
                    );
                    return;
                  }

                  Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                    return CookbookListPage(value.trim(), -1);
                  }));

                  _searchController.clear();
                },
                maxLines: 1,
                textInputAction: TextInputAction.search,
                autofocus: false,
                decoration: InputDecoration(
                    labelText: "搜索菜谱",
                    border: OutlineInputBorder()
                ),
                controller: _searchController,
              ),

              RecommendWidget()
            ],
          ),
        ),
      ),
    );
  }

}

class RecommendWidget extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _Recommend();
  }

}

class _Recommend extends State {

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 8),
      child: Wrap(
        spacing: 4,
        children: _list,
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    if (_list.isEmpty) {
      getData();
    }
  }

  void getData() async {
    String json = await loadRecommendFromAsset();
    List result = jsonDecode(json);
    //print(result);
    result.shuffle();

    setState(() {
      for(int i = 0; i < result.length / 3; i++) {
        _list.add(
            GestureDetector(
              child: Chip(
                label: Text(result[i]),
                key: Key(result[i]),
              ),
              onTap: () {
                //print("$i tap");

                Navigator.of(_context).push(MaterialPageRoute(builder: (context) {
                  return CookbookListPage(result[i], -1);
                }));
              },
            )

        );
      }
    });

  }

  Future<String> loadRecommendFromAsset() async {
    return await rootBundle.loadString('json/recommend.json');
  }

}

